'use strict'
let Random = require('../utils/random')

exports.attacks = function(p1, p2) {
  let random = new Random()
  let fightAttri1 = p1.initFightAttri()
  let fightAttri2 = p2.initFightAttri()
  let damage = 0
  let fightMsg = {
    p1: {
      fightAttri: p1.getFightAttriCopy(),
      name: p1.name,
    },
    p2: {
      fightAttri: p2.getFightAttriCopy(),
      name: p2.name,
    },
    result: 0,
    seed: random.seed,
  }

  let skillLen = fightAttri1.attackSkill.length + fightAttri1.defendSkill.length
  if (skillLen > 0) {
    fightAttri1['perSkillRate'] = fightAttri1.skillRate / (fightAttri1.attackSkill.length + fightAttri1.defendSkill.length)
  } else {
    fightAttri1['perSkillRate'] = 0
  }

  skillLen = fightAttri2.attackSkill.length + fightAttri2.defendSkill.length
  if (skillLen > 0) {
    fightAttri2['perSkillRate'] = fightAttri2.skillRate / (fightAttri2.attackSkill.length + fightAttri2.defendSkill.length)
  } else {
    fightAttri2['perSkillRate'] = 0
  }
  while (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
    calcAttack(fightAttri1, fightAttri2, random)
    if (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
      calcAttack(fightAttri2, fightAttri1, random)
    }
  }

  //result = 1 胜利 0失败
  if (fightAttri2.hp <= 0) {
    fightMsg.result = 1
  }
  return fightMsg
}

exports.attackPlayer = function(p1, p2) {
  let random = new Random()
  let fightAttri1 = p1.getAllFightAttriCopy()
  let fightAttri2 = p2.getAllFightAttriCopy()
  let damage = 0
  let fightMsg = {
    p1: {
      fightAttri: p1.getAllFightAttriCopy(),
      name: p1.name,
    },
    p2: {
      fightAttri: p2.getAllFightAttriCopy(),
      name: p2.name,
    },
    result: 0,
    seed: random.seed,
  }

  let skillLen = fightAttri1.attackSkill.length + fightAttri1.defendSkill.length
  if (skillLen > 0) {
    fightAttri1['perSkillRate'] = fightAttri1.skillRate / (fightAttri1.attackSkill.length + fightAttri1.defendSkill.length)
  } else {
    fightAttri1['perSkillRate'] = 0
  }

  skillLen = fightAttri2.attackSkill.length + fightAttri2.defendSkill.length
  if (skillLen > 0) {
    fightAttri2['perSkillRate'] = fightAttri2.skillRate / (fightAttri2.attackSkill.length + fightAttri2.defendSkill.length)
  } else {
    fightAttri2['perSkillRate'] = 0
  }

  while (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
    calcAttack(fightAttri1, fightAttri2, random)
    if (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
      calcAttack(fightAttri2, fightAttri1, random)
    }
  }

  //result = 1 胜利 0失败
  if (fightAttri2.hp <= 0) {
    fightMsg.result = 1
  }
  return fightMsg
}

exports.attackFieldBoss = function(p1, p2) {
  let random = new Random()
  let fightAttri1 = p1.initFightAttri()
  let fightAttri2 = p2.initFightAttri()
  let damage = 0
  let fightMsg = {
    p1: {
      fightAttri: p1.getFightAttriCopy(),
      name: p1.name,
    },
    p2: {
      fightAttri: p2.getFightAttriCopy(),
      name: p2.name,
    },
    result: 0,
    seed: random.seed,
  }
  let skillLen = fightAttri1.attackSkill.length + fightAttri1.defendSkill.length
  if (skillLen > 0) {
    fightAttri1['perSkillRate'] = fightAttri1.skillRate / (fightAttri1.attackSkill.length + fightAttri1.defendSkill.length)
  } else {
    fightAttri1['perSkillRate'] = 0
  }

  skillLen = fightAttri2.attackSkill.length + fightAttri2.defendSkill.length
  if (skillLen > 0) {
    fightAttri2['perSkillRate'] = fightAttri2.skillRate / (fightAttri2.attackSkill.length + fightAttri2.defendSkill.length)
  } else {
    fightAttri2['perSkillRate'] = 0
  }


  let fieldBossDamage = 0
  while (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
    damage = calcAttack(fightAttri1, fightAttri2, random)
    fieldBossDamage += damage
    if (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
      calcAttack(fightAttri2, fightAttri1, random)
    }
  }

  if (fightAttri2.hp <= 0) {
    fightMsg.result = 1
  }
  fightMsg.fieldBossDamage = fieldBossDamage
  return fightMsg
}

//计算攻击
function calcAttack(attacker, defender, random) {
  let damage = 0
  if (attacker.debuff === 1) {
    attacker.debuff = 0
    return damage
  }

  // 计算防御者技能触发
  let defendSkillRate = random.getRand()
  if (defendSkillRate < defender.perSkillRate * defender.defendSkill.length) {
    let index = Math.floor(defendSkillRate / defender.perSkillRate)
    let defendSkillId = defender.defendSkill[index]
    damage = defendSkills[defendSkillId](attacker, defender, random)
    return damage
  } else {
    damage = calcAttackSkill(attacker, defender, random)
    return damage
  }
}

function calcAttackSkill(attacker, defender, random, defendPer) {
  let damage = 0
    //计算攻击者技能触发
  let attackSkillRate = random.getRand()
  if (attackSkillRate < attacker.perSkillRate * attacker.attackSkill.length) {
    let index = Math.floor(attackSkillRate / attacker.perSkillRate)
    let attackSkillId = attacker.attackSkill[index]
    var j = 1
    if (attackSkillId === 450001 || attackSkillId === 450002) {//连击
      j = 2
    }

    for (var k = 0; k < j; k++) {
      let damage1 = Math.ceil(attackSkills[attackSkillId](attacker, defender, random) * (1 + attacker.skillHurtPer / 100))
      if(damage1 > 0){
        if (defendPer) {
          damage1 = Math.ceil(damage1 * defendPer)
        }
        defender.hp -= damage1
        if (defender.hp < 0) {
          damage1 += defender.hp
          defender.hp = 0
        }
        damage += damage1
      }
    }
    if (attackSkillId === 450010) { //吸血
      attacker.hp += Math.ceil(damage * 0.40)
      if (attacker.hp > attacker.maxHp) {
        attacker.hp = attacker.maxHp
      }
    } else if (attackSkillId === 450011) { //吸血
      attacker.hp += Math.ceil(damage * 0.50)
      if (attacker.hp > attacker.maxHp) {
        attacker.hp = attacker.maxHp
      }
    }

  } else {
    damage = Math.ceil(calcDamage(attacker, defender, random))
    if (defendPer) {
      damage = Math.ceil(damage * defendPer)
    }
    defender.hp -= damage
    if (defender.hp < 0) {
      damage += defender.hp
      defender.hp = 0
    }
  }
  return damage
}

//计算伤害
function calcDamage(attacker, defender, random) {
  let defend = defender.defend - attacker.ignoreDefend
  if (defend < 0) {
    defend = 0
  }
  let damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
    //计算暴击
  if (random.getRand() < attacker.critRate) {
    damage *= (1 + attacker.critHurt / 100)
  }
  if (damage <= 0) {
    damage = 1
  }
  return Math.ceil(damage)
}

//攻击技能
let attackSkills = {
  //连击: 造成两次攻击, 单次伤害降低40%
  450001: function skill_450001(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    return damage * 0.60
  },
  //连击: 造成两次攻击, 单次伤害降低30%
  450002: function skill_450002(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    return damage * 0.70
  },
  //嗜血: 造成伤害的时候，将50%的伤害转化为自己的生命。
  450010: function skill_450010(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    return damage
  },
  //嗜血: 造成伤害的时候，将50%的伤害转化为自己的生命。
  450011: function skill_450011(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    return damage
  },
  //重击: 造成30%的伤害，并击晕敌人。
  450020: function skill_450020(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    defender.debuff = 1
    return damage * 0.3
  },
  //重击: 造成45%的伤害，并击晕敌人。
  450021: function skill_450021(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    defender.debuff = 1
    return damage * 0.45
  },
  //春风: 恢复自身15%血量
  450030: function skill_450030(attacker, defender, random) {
    attacker.hp += Math.ceil(attacker.maxHp * 0.15 * (1 + attacker.skillHurtPer / 100))
    if (attacker.hp > attacker.maxHp) {
      attacker.hp = attacker.maxHp
    }
    return 0
  },
  //进阶春风: 恢复自身20%血量
  450031: function skill_450031(attacker, defender, random) {
    attacker.hp += Math.ceil(attacker.maxHp * 0.2 * (1 + attacker.skillHurtPer / 100))
    if (attacker.hp > attacker.maxHp) {
      attacker.hp = attacker.maxHp
    }
    return 0
  },
  //斩杀:提升30暴击率, 和20%的暴击伤害
  450040: function skill_450040(attacker, defender, random) {
    let defend = defender.defend - attacker.ignoreDefend
    if (defend < 0) {
      defend = 0
    }
    let damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
      //计算暴击
    if (random.getRand() * 100 < (attacker.critRate + 30)) {
      damage *= 1 + (attacker.critHurt + 20) / 100
    }
    return Math.ceil(damage)
  },
  //进阶斩杀:提升30暴击率, 和40%的暴击伤害
  450041: function skill_450041(attacker, defender, random) {
    let defend = defender.defend - attacker.ignoreDefend
    if (defend < 0) {
      defend = 0
    }
    let damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
      //计算暴击
    if (random.getRand() * 100 < (attacker.critRate + 30)) {
      damage *= 1 + (attacker.critHurt + 40) / 100
    }
    return Math.ceil(damage)
  },
  //凝风: 造成120%的伤害
  450050: function skill_450050(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    damage *= 1.2
    return damage
  },
  //山崩: 造成130%的伤害
  450051: function skill_450051(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    damage *= 1.3
    return damage
  },
  //烈日: 造成140%的伤害
  450052: function skill_450052(attacker, defender, random) {
    let damage = calcDamage(attacker, defender, random)
    damage *= 1.4
    return damage
  },

  //雨刃: 造成等级*6的固定伤害
  450060: function skill_450060(attacker, defender, random) {
    let damage = attacker.lvl * 6
    return damage
  },
  //烈日: 造成等级*7的固定伤害
  450061: function skill_450061(attacker, defender, random) {
    let damage = attacker.lvl * 7
    return damage
  },
  //雷击: 造成等级*8的固定伤害
  450062: function skill_450062(attacker, defender, random) {
    let damage = attacker.lvl * 8
    return damage
  },
}

let defendSkills = {
  //灵敏:闪避敌人的攻击
  450101: function skill_450101(attacker, defender, random) {
    return 0
  },
  //防御: 降低 30%的伤害
  450110: function skill_450110(attacker, defender, random) {
    let damage = calcAttackSkill(attacker, defender, random, 0.7)
    return damage
  },
  //防御: 降低 50%的伤害
  450111: function skill_450111(attacker, defender, random) {
    let damage = calcAttackSkill(attacker, defender, random, 0.5)
    return damage
  },
  //反击: 反击对手，反击伤害为攻击伤害的40%。
  450120: function skill_450120(attacker, defender, random) {
    let damage = calcAttackSkill(attacker, defender, random)
    if (damage > 0 && defender.hp > 0) {
      let counterDamage = calcDamage(defender, attacker, random)
      attacker.hp -= Math.ceil(counterDamage * 0.4)
      if (attacker.hp < 0) {
        attacker.hp = 0
      }
    }
    return damage
  },
  //反击: 反击对手，反击伤害为攻击伤害的60%。
  450121: function skill_450121(attacker, defender, random) {
    let damage = calcAttackSkill(attacker, defender, random)
    if (damage > 0 && defender.hp > 0) {
      let counterDamage = calcDamage(defender, attacker, random)
      attacker.hp -= Math.ceil(counterDamage * 0.6)
      if (attacker.hp < 0) {
        attacker.hp = 0
      }
    }
    return damage
  },
}
